package com.longer.playhelper.view.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

import com.longer.playhelper.R;
import com.longer.playhelper.utlis.FileTools;
import com.longer.playhelper.utlis.Toast;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class SelectSchoolActivity extends AppCompatActivity {

    private Context context;
    private EditText ed_select;
    private ListView lv;
    private static String ed_text;
    String[] school;
    String[] school_select;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_selectschool);

        init();
    }

    private void init() {
        context = SelectSchoolActivity.this;
        ed_select = (EditText) findViewById(R.id.frag_query_et);
        findViewById(R.id.iv_que_clear).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ed_select.setText("");
            }
        });
        findViewById(R.id.iv_que_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        lv = (ListView) findViewById(R.id.library_lv_select);
        ed_select.addTextChangedListener(textchange);
        select();
    }

    class ClickItem implements OnItemClickListener {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//            Toast.show("点击了：" + school_select[position]);
            FileTools.saveshareString("selectSchool", school_select[position]);
            finish();
        }

    }

    private TextWatcher textchange = new TextWatcher() {
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // System.out.println("-1-onTextChanged-->" +
            // ed_select.getText().toString() + "<--");
        }

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            // System.out.println("-2-onTextChanged-->" +
            // ed_select.getText().toString() + "<--");
        }

        // 监听搜索框文字的变化，用这个方法
        public void afterTextChanged(Editable s) {
            ed_text = ed_select.getText().toString().trim();
            System.out.println("输入之后：" + ed_text + "<--");
            select();
        }
    };

    /**
     * 检索学校
     */
    public void select() {
        try {
            InputStream is = getAssets().open("school.txt");
            int size = is.available();

            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();

            String text = new String(buffer, "unicode");
            school = text.split("\n");

            List<String> list = new ArrayList<String>();
            if (ed_text == null) {
                for (String name : school) {
                    list.add(name);
                }
            } else {
                for (String name : school) {
                    if (name.contains(ed_text)) {
                        list.add(name);
                    }
                }
            }
            school_select = new String[list.size()];
            list.toArray(school_select);

            lv.setOnItemClickListener(new ClickItem());


            setdate(school);
        } catch (IOException e) {
            e.printStackTrace();
            Toast.show("读取学校出错啦~");
        }
    }

    private void setdate(String[] school) {
        try {
            lv.setAdapter(new ArrayAdapter<String>(context, R.layout.layout_selectschool, R.id.score_kscx_tv, school_select));
        } catch (Exception e) {
            e.printStackTrace();
            Toast.show("读取学校出错啦~");
        }
    }

}
